<?php

namespace App\Http\Controllers\Api;

use App;
use App\Util\Common;
use App\Util\GetMac;
use App\Util\SqlServerUtil;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\Debug\header;
use Illuminate\Support\Facades\Storage;
use Excel;

class SCRegrController extends CommonController
{
    protected $response = [
    		"Error" => 0,
    		"Message" => "Success!",
    		"Data" => []
	];
    
    
    /**
     * 燃油保存
     * @date: 2022年12月29日 下午3:18:57
     * @author: juyi
     */
    public function SCRegrSave(Request $request){
    	$data = $request ->data ; //
    	if(empty($data)  ){
    		showMsg(400,'错误，请选择一行');
    	}
    	$fmodel = $data['fmodel'];
    	$fbillno = $data['fbillno'];
//     	dd( $data );
    	$fisjiachun = $request ->fisjiachun??1 ;
    	//1-不是甲醇机 。2--甲醇机
    	$chuanjianfSql =" ";
    	$zdsql ='';
    	if($data['fchuanjishe'] == 'CCS'){
    		$chuanjianfSql =" left join gz_YchuanjianfCCS cjs on cjs.fmodel = v1.fmodel" ;
    		$zdsql =" isnull(cjs.fnoxpaifangf,0) as fchaunjishefy " ;
    	}elseif($data['fchuanjishe'] == 'ABS'){
    		$chuanjianfSql =" left join gz_YchuanjianfABS cjs on cjs.fmodel = v1.fmodel" ;
    		$zdsql =" isnull(cjs.fnoxpaifangfRMB,0) as fchaunjishefy " ;
    	}elseif($data['fchuanjishe'] == 'LR'){
    		$chuanjianfSql =" left join gz_YchuanjianfLR cjs on cjs.fmodel = v1.fmodel" ;
    		$zdsql =" isnull(cjs.fnoxpaifangf,0) as fchaunjishefy " ;
    	}elseif($data['fchuanjishe'] == 'BV'){
    		$chuanjianfSql =" left join gz_YchuanjianfBV cjs on cjs.fmodel = v1.fmodel" ;
    		$zdsql =" isnull(cjs.fnoxpaifangf,0) as fchaunjishefy " ;
    	}elseif($data['fchuanjishe'] == 'DNV'){
    		$chuanjianfSql =" left join gz_YchuanjianfDNV cjs on cjs.fmodel = v1.fmodel" ;
    		$zdsql =" isnull(cjs.fnoxpaifangf,0) as fchaunjishefy " ;
    	}elseif($data['fchuanjishe'] == 'NK'){
    		$chuanjianfSql =" left join gz_YchuanjianfNK cjs on cjs.fmodel = v1.fmodel" ;
    		$zdsql =" isnull(cjs.fnoxpaifangf,0) as fchaunjishefy " ;
    	}else{
    	   $aa =  SqlServerUtil::execTosqlOK($this->SqlServer,  "select 1 from gz_YchuanjianfQT where fjybjbillno = '{$fbillno}' and  FStatus =1  " );
    	   if(empty($aa)){
    	       showMsg(400,"流水号：{$fbillno},船检费其他列表，订单未审核 或 未生成，请检查。" );
    	    }
    	    $chuanjianfSql =" left join gz_YchuanjianfQT  cjs on  cjs.fjybjbillno = v1.fbillno  " ;
    		$zdsql = "  isnull(cjs.fpaifangmuxing,0)  as fchaunjishefy " ;
    	}
    	    	
    	
    	$sql="  select v1.fbillno,v1.fmodel,v1.fsxtaishu,v1.fqty,v1.fsjpower,v1.fhtpower,v1.fchuanjishe,v1.fzhujiranliaos,
		v1.fisryrq,v1.fisscr,v1.fscrlowup,v1.fisegr,v1.fegrlowup,v1.fmpctriton,
		u1.fbrand,u1.fgas as franqi,u1.fdieseloil as franyou,u1.fcylinderoil as fqigangyou,u1.fhanjiegs,u1.fjijiags,
		u1.fzhuangshigs,u1.fegrgs,u1.fscrgs,u1.fftfscr,u1.fftfegr,u1.fftfiscr,u1.ffuliao,u1.fpackage,u1.ffuliaopakeage, 
		isnull(icer.ficertjgzfy,0) as ficertjgzfy ,isnull(icer.fgdzjqq,0) as fgdzjqq, isnull(icer.ficergs,0) as ficergs,
		 {$zdsql} 
		,ryhz.fbillno as fbillnoryhz, 
		ryhz.fstranyoucgj,ryhz.fstranyoucgjscr,ryhz.fstranyoucgjegr,
		ryhz.fstranyousyldf,ryhz.fstranyousylicer,
		ryhz.fstranqisyldf,ryhz.fstranqisylicer,ryhz.fstqigangyoucgj,
		ryhz.fstqigangyoucgjscr,
		ryhz.fstqigangyoucgjegr,ryhz.fstqigangyousyldf,ryhz.fstqigangyousylicerry,
		ryhz.fstqigangyousylicerrq,fstniaosucgjscr,ryhz.fstnaohcgjegrbp,
		ryhz.fstnaohcgjecoegr,ryhz.fstnaohsrlicerry,ryhz.fstnaohsrlicerrq,

		ryhz.fnostranyoucgj,ryhz.fnostranyoucgjscr,ryhz.fnostranyoucgjegr,
		ryhz.fnostranyousyldf,ryhz.fnostranyousylicer,
		ryhz.fnostranqisyldf,ryhz.fnostranqisylicer,ryhz.fnostqigangyoucgj,
		ryhz.fnostqigangyoucgjscr,
		ryhz.fnostqigangyoucgjegr,ryhz.fnostqigangyousyldf,ryhz.fnostqigangyousylicerry,
		ryhz.fnostqigangyousylicerrq,fnostniaosucgjscr,ryhz.fnostnaohcgjegrbp,
		ryhz.fnostnaohcgjecoegr,ryhz.fnostnaohsrlicerry,ryhz.fnostnaohsrlicerrq
		from gz_YBaseSC  v1  inner join gz_YBaseJS u1 on v1.fmodel = u1.fmodel 
		left join gz_YranyouHZ ryhz on  ryhz.fbillno = v1.fbillno and v1.fmodel = ryhz.fmodel 
		left join gz_YjzICERDB icer on  v1.fEGCModel = icer.fEGCModel 
		$chuanjianfSql	where 1=1 and v1.fmodel ='{$fmodel}' and v1.fbillno ='{$fbillno}' " ;
// 		echo $sql;die;
// 	    $sql = str_replace('{$chuanjianf}', $chuanjianfSql, $sql);
    	$result = SqlServerUtil::execTosqlOK($this->SqlServer,  $sql,2);
    	
//     	dd($result['fchaunjishefy'] );
    	//判断 燃油汇总 是否 已生成
    	if(empty($result['fbillnoryhz'])){
    		showMsg(400,'流水号：'.$result['fbillno'].'，还没有生成燃油汇总，请技术确认');
    	}
    	//参数
    	$CsData = $this->getSystemProfile(['fsetzhuhunyouprice','fsetqigangprice','fsetranqiprice',
    			'fsetchaiyouprice','fsetfanyingjiprice','fsetzhuangshirgprice','fSetNaOHPrice',
    			'FSetDftfSCR1','FSetDianFei','FSetPfcsfSCR','FSetPfcsfEGR','FSetPfcsfICER','FSetRGzhuangshiCJPrice'
    	]);
//     	dd($CsData);
    	$ZhuHunYouPrice = $CsData['fsetzhuhunyouprice'] ;//主滑油单价
    	$QiGangPrice = $CsData['fsetqigangprice'] ;//气缸单价
    	$RanqiPrice = $CsData['fsetranqiprice'] ;//燃气单价
    	$ChaiYouPrice = $CsData['fsetchaiyouprice'] ;//柴油单价
    	$FanYingJiPrice = $CsData['fsetfanyingjiprice'] ;//反应剂的单价
    	$fsetZhuangShiRGprice = $CsData['fsetrgzhuangshicjprice'] ; //装试人工单价 ,装试车间单价
    	$fnaohPrice =  $CsData['fsetnaohprice']; //NaOH 单价
    	$fDftfscr1 =  $CsData['fsetdftfscr1']; // 待分摊的费用-SCR1
    	$fdianfei  =  $CsData['fsetdianfei']; //电费
    	$FpfcsfSCR  =  $CsData['fsetpfcsfscr']; //排放测试费scr
    	$FpfcsfEGR  =  $CsData['fsetpfcsfegr']; //排放测试费EGR
    	$FpfcsfICER  =  $CsData['fsetpfcsficer']; //排放测试费 ICER
    	//判断这里如果生效台数大于4的，分摊的台数是4；小于等于4，按照实际生效台数
    	$sxfqtyTmp = $result['fqty'] ;
    	if($result['fqty']>4){
    		$sxfqtyTmp = 4 ;
    	}
    	// fisSCR  -- HPSCR ,LPSCR ,否
    	// fSCRlowup  -- 高硫,  低硫,否
    	// fisEGR : EcoEGR , EGRBP, 否无
    	// fEGRlowup -- 高硫,  低硫,否
    	$SCR27001['fAllAmountSCR']= 0 ;
    	$SCR27001['fAllAmountEGR']= 0 ;
    	//ECOEGR
    	$SCR27012['fAllAmount'] = 0 ;
    	//燃油版
    	$SCR27015['fAllAmount']= 0 ;
    	//燃气 版
    	$SCR27006['fAllAmount']= 0;
    	// fisryrq  -- 燃油 ， 燃气 ，无  否 
    	//-- 判断 SCR / EGR 只有其一
//     	if($result['fisscr'] !='否' || $result['fisscr'] !='无'){
    		//有 SCR
    		// SCR/EGR油气首台  27009   ==  gz_YjzSCREGRst
    		// SCR/EGR油气非首台  27008  ==  gz_YjzSCREGRNost
    		// 	SCR/EGR除采购成本汇总  27001  == gz_YjzSCREGRNOcbHZ
    		// SCR/EGR油气首台
    		$ranyouData = getRanYouQiHZ($result) ;
    		if($result['fisscr'] == 'iSCR' ){
    			$result['fftfscr'] =  $result['fftfiscr'];
    		}
    		
//     		dd($ranyouData ,$result['fisryrq'] , $result['fisscr'] , $result['fisegr'] );
    		if(in_array(strtoupper($result['fisscr']),['HPSCR' , 'LPSCR','ISCR'])  || $result['fisegr'] == 'EGRBP'){
	    		$SCR27009 =[
	    				'fbillno'=>$fbillno,
	    				'fmodel'=>$fmodel,
	    				'fcyQty'  => $ranyouData['ranyou'],
	    				'fcyPrice'=> $ChaiYouPrice,
	    				'fcyAmount'=>$ranyouData['ranyou']*$ChaiYouPrice,
	    				
	    				'fqgyQty' => $ranyouData['qigang'],
	    				'fqgyPrice'=>$QiGangPrice,
	    				'fqgyAmount'=>$ranyouData['qigang']*$QiGangPrice ,
	    				
	    				'ftrqQty'  => $ranyouData['ranqi'],
	    				'ftrqPrice'=> $RanqiPrice,
	    				'ftrqAmount'=> $ranyouData['ranqi'] * $RanqiPrice,
	    				'fAllAmount'=> round($ranyouData['ranyou']*$ChaiYouPrice + $ranyouData['qigang']*$QiGangPrice+ $ranyouData['ranqi'] * $RanqiPrice,2),
	    				
	    		 ];
// 	    		dd($SCR27009 ,$ranyouData,  $result['fisryrq'] , $result['fisscr'] ,$result['fisegr'] );
	    		 // SCR/EGR油气非首台
	    		 $SCR27008 =[
	    		 		'fbillno'=>$fbillno,
	    		 		'fmodel'=>$fmodel,
	    		 		'fcyQty'  => $ranyouData['noranyou'],
	    		 		'fcyPrice'=> $ChaiYouPrice,
	    		 		'fcyAmount'=>$ranyouData['noranyou']*$ChaiYouPrice,
	    		 		
	    		 		'fqgyQty' => $ranyouData['noqigang'],
	    		 		'fqgyPrice'=>$QiGangPrice,
	    		 		'fqgyAmount'=>$ranyouData['noqigang']*$QiGangPrice ,
	    		 		
	    		 		'ftrqQty'  => $ranyouData['noranqi'],
	    		 		'ftrqPrice'=> $RanqiPrice,
	    		 		'ftrqAmount'=> $ranyouData['noranqi'] * $RanqiPrice,
	    		 		'fAllAmount'=> round($ranyouData['noranyou']*$ChaiYouPrice + $ranyouData['noqigang']*$QiGangPrice+ $ranyouData['noranqi'] * $RanqiPrice,2),
	    		 		
	    		 		
	    		 ];
	//     		 dd($SCR27008 , $SCR27009);
	    		 //油耗 （SCR或者EGR油气首台!K2*1+(生效台数-1）*SCR或者EGR油气非首台!K2）/生效台数
	    		 $youhaoSCR =round( ($SCR27009['fAllAmount'] + ($result['fqty']-1)* $SCR27008['fAllAmount']  )/$result['fqty'],2) ;
	    		 //证书费取船级社的NOX排放费的金额
	    		 $zhengshuf = 0 ;
	    		 // $FpfcsfEGR    $FpfcsfSCR 
	    		 
	    		 $SCR27001 =[
	    		 		'fbillno'=>$fbillno,
	    		 		'fmodel'=> $fmodel,
	    		 		'fyouhao'=> $youhaoSCR,
	    		 		'ffyjQty'=> $ranyouData['niaosu'], 
	    		 		'ffyjprice'=> $FanYingJiPrice,
	    		 		'ffyjAmount'=> $ranyouData['niaosu']*$FanYingJiPrice,
	    		 		
	    		 		'fftQty'=>$result['fqty'],
	    		 		'fftfyjAmount'=>round($ranyouData['niaosu']*$FanYingJiPrice/$result['fqty'],2),  //反应剂的金额	/分摊的台数
	    		 		
	    		 		'fnaohqty'=>$ranyouData['naoh'],
	    		 		'fnaohPrice'=>$fnaohPrice,
	    		 		'fnaohAmount'=>$ranyouData['naoh']* $fnaohPrice,
	    		 		
	    		 		'fgongshi'=> $result['fscrgs'],
	    		 		'frengongcb'=> $result['fscrgs']*$fsetZhuangShiRGprice ,
	    		 		
	    		 		'fgongshiEGR'=> $result['fegrgs'],
	    		 		'frengongcbEGR'=> $result['fegrgs']*$fsetZhuangShiRGprice ,
	    		 		
	    		 		'fdftfeiyongSCR1'=> $fDftfscr1 ,
	    		 		'fdftfeiyongSCR2'=> $result['fftfscr'],
	    		 		'ffttaishu1'=> $sxfqtyTmp,
	    		 		'fftfeiyongSCR'=>  round($result['fftfscr']/$sxfqtyTmp ,2),
	    		 		
	    		 		'fdftfeiyongEGR'=> $result['fftfegr'] ,
	    		 		'ffttaishu2'=> $sxfqtyTmp ,
	    		 		'fftfeiyongEGR'=> round($result['fftfegr']/$sxfqtyTmp ,2) ,
	    		 		
	    		 		'fpfcsfei'=> $FpfcsfSCR ,
	    		 		'fpfcsfeiEGR'=> $FpfcsfEGR ,
	    		 		'fzsfei'=>$result['fchaunjishefy'],
	    		 		'fdfttaishu'=>  $result['fqty'],
	    		 		
	    		 		'fftpfcsfei'=>round(($FpfcsfSCR+$result['fchaunjishefy'])/$result['fqty'] ,2), //fpfcsfei+ fzsfei   ==暂时
	    		 		'fftpfcsfeiEGR'=>round(($FpfcsfEGR+$result['fchaunjishefy'])/$result['fqty'] ,2), //fpfcsfei+ fzsfei   ==暂时
	    		 		
	    		 		'fdianfei'=>$fdianfei,
	    		 ];
// 	    		 dd($result , $SCR27001 ,$FpfcsfEGR , $fsetZhuangShiRGprice);
	    		 $SCR27001['fAllAmountSCR']= $SCR27001['fyouhao']+ $SCR27001['fftfyjAmount']+ $SCR27001['frengongcb']
	    		 + $SCR27001['fdftfeiyongSCR1']+ $SCR27001['fftfeiyongSCR']+ $SCR27001['fftpfcsfei']+ $SCR27001['fdianfei'];
	    		 
	    		 $SCR27001['fAllAmountEGR']=  $SCR27001['fyouhao']+ $SCR27001['fnaohAmount']+ $SCR27001['frengongcbEGR']
	    		 + $SCR27001['fftfeiyongEGR']+ $SCR27001['fftpfcsfeiEGR']+ $SCR27001['fdianfei'];
	    		 //SCR  =  fyouhao + fftfyjAmount + frengongcb + fdftfeiyongSCR1 + fftfeiyongSCR + fftpfcsfei+ fdianfei
	    		 //EGR = fyouhao +fnaohAmount + frengongcb+ fftfeiyongEGR + fftpfcsfei+ fdianfei
// 	    		 dd($SCR27001, $SCR27008,$SCR27009);
	
	    		 SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzSCREGRNOcbHZ' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
	    		 SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzSCREGRst' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
	    		 SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzSCREGRNost' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
	    		 
	    		 SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzSCREGRNOcbHZ' , $SCR27001);
	    		 SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzSCREGRst' , $SCR27009);
	    		 SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzSCREGRNost' , $SCR27008);

    		}
    		
    		// ECOEGR 
    		/* EcoGR除采购成本外汇总    27012
    		gz_YjzECOEGRNOcbHZ 
    		ECOEGR油气首台    27013
    		gz_YjzECOEGRst
    		ECOEGR油气非首台    27014
    		gz_YjzECOEGRNost */
    		if(strtoupper($result['fisegr']) == 'ECOEGR'){
    			$SCR27013 =[
    					'fbillno'=>$fbillno,
    					'fmodel'=>$fmodel,
    					'fcyQty'  => $ranyouData['ranyou'],
    					'fcyPrice'=> $ChaiYouPrice,
    					'fcyAmount'=>$ranyouData['ranyou']*$ChaiYouPrice,
    					
    					'fqgyQty' => $ranyouData['qigang'],
    					'fqgyPrice'=>$QiGangPrice,
    					'fqgyAmount'=>$ranyouData['qigang']*$QiGangPrice ,
    					
    					'ftrqQty'  => $ranyouData['ranqi'],
    					'ftrqPrice'=> $RanqiPrice,
    					'ftrqAmount'=> $ranyouData['ranqi'] * $RanqiPrice,
    					'fAllAmount'=> round($ranyouData['ranyou']*$ChaiYouPrice + $ranyouData['qigang']*$QiGangPrice+ $ranyouData['ranqi'] * $RanqiPrice,2),
    					
    			];
    			
    			//     		dd($SCR27009 , $result['fisryrq'] , $result['fisscr'] ,$result['fisegr'] );
    			// EcoGR油气非首台
    			$SCR27014 =[
    					'fbillno'=>$fbillno,
    					'fmodel'=>$fmodel,
    					'fcyQty'  => $ranyouData['noranyou'],
    					'fcyPrice'=> $ChaiYouPrice,
    					'fcyAmount'=>$ranyouData['noranyou']*$ChaiYouPrice,
    					
    					'fqgyQty' => $ranyouData['noqigang'],
    					'fqgyPrice'=>$QiGangPrice,
    					'fqgyAmount'=>$ranyouData['noqigang']*$QiGangPrice ,
    					
    					'ftrqQty'  => $ranyouData['noranqi'],
    					'ftrqPrice'=> $RanqiPrice,
    					'ftrqAmount'=> $ranyouData['noranqi'] * $RanqiPrice,
    					'fAllAmount'=> round($ranyouData['noranyou']*$ChaiYouPrice + $ranyouData['noqigang']*$QiGangPrice+ $ranyouData['noranqi'] * $RanqiPrice,2),
    					
    					
    			];
    			//     		 dd($SCR27013 , $SCR27014 );
    			//油耗 （SCR或者EGR油气首台!K2*1+(生效台数-1）*SCR或者EGR油气非首台!K2）/生效台数
    			$youhaoECOEGR =round( ($SCR27013['fAllAmount'] + ($result['fqty']-1)* $SCR27014['fAllAmount']  )/$result['fqty'],2) ;
    			//证书费取船级社的NOX排放费的金额
    			$zhengshuf = 0 ;
    			$SCR27012 =[
    					'fbillno'=>$fbillno,
    					'fmodel'=> $fmodel,
    					'fyouhao'=> $youhaoECOEGR ,
    					'fnaohqty'=>$ranyouData['naoh'],
    					'fnaohPrice'=>$fnaohPrice,
    					'fnaohAmount'=>$ranyouData['naoh']* $fnaohPrice,
    					
    					'fgongshi'=> $result['fegrgs'],
    					'frengongcb'=> $result['fegrgs']*$fsetZhuangShiRGprice ,
    					 
    					'fdftfeiyongegr'=> $result['fftfegr'] ,
    					'ffttaishu'=> $sxfqtyTmp ,
    					'fftfeiyongegr'=> round($result['fftfegr']/$sxfqtyTmp ,2) ,
    					'fpfcsfei'=> $FpfcsfEGR ,
    					'fzsfei'=>$result['fchaunjishefy'],  // fzsfei
    					'fdfttaishu'=>  $result['fqty'],
    					
    					'fftpfcsfei'=>round(($FpfcsfEGR+$result['fchaunjishefy'])/$result['fqty'],2), //fpfcsfei+ fzsfei   ==暂时
    					'fdianfei'=>$fdianfei,
    			];
    			//fyouhao +fnaohAmount+ frengongcb+ fftfeiyongegr + fftpfcsfei+ fdianfei
    			$SCR27012['fAllAmount']= $SCR27012['fyouhao']+ $SCR27012['fnaohAmount']+ $SCR27012['frengongcb']
    			+ $SCR27012['fftfeiyongegr']+  $SCR27012['fftpfcsfei']+ $SCR27012['fdianfei'];
    			
//     			  dd($SCR27012, $SCR27013,$SCR27014);
    			SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzECOEGRNOcbHZ' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    			SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzECOEGRst' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    			SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzECOEGRNost' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    			
    			SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzECOEGRNOcbHZ' , $SCR27012);
    			SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzECOEGRst' , $SCR27013);
    			SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzECOEGRNost' , $SCR27014);
    			
    		}
    		 
    		//燃油版
    		/* ICER燃油版油气  27002
    		gz_YjzICERrybyouqist
    		
    		ICER燃油版油气非首台  27003
    		gz_YjzICERrybyouqiNOst
    		r燃油版汇总  27015
    		gz_YjzICERRanYouHZ */
    		if($result['fisryrq'] == '燃油'){
    			$SCR27002 =[
    					'fbillno'=>$fbillno,
    					'fmodel'=>$fmodel,
    					'fcyQty'  => $ranyouData['ranyou'],
    					'fcyPrice'=> $ChaiYouPrice,
    					'fcyAmount'=>$ranyouData['ranyou']*$ChaiYouPrice,
    					
    					'fqgyQty' => $ranyouData['qigang'],
    					'fqgyPrice'=>$QiGangPrice,
    					'fqgyAmount'=>$ranyouData['qigang']*$QiGangPrice ,
    					
    					'ftrqQty'  => $ranyouData['ranqi'],
    					'ftrqPrice'=> $RanqiPrice,
    					'ftrqAmount'=> $ranyouData['ranqi'] * $RanqiPrice,
    					'fAllAmount'=> round($ranyouData['ranyou']*$ChaiYouPrice + $ranyouData['qigang']*$QiGangPrice+ $ranyouData['ranqi'] * $RanqiPrice,2),
    					
    			];
//     			dd( $result['fisryrq'] , $result['fisscr'] ,$result['fisegr'] );
    			// 燃油版 非首台
    			$SCR27003 =[
    					'fbillno'=>$fbillno,
    					'fmodel'=>$fmodel,
    					'fcyQty'  => $ranyouData['noranyou'],
    					'fcyPrice'=> $ChaiYouPrice,
    					'fcyAmount'=>$ranyouData['noranyou']*$ChaiYouPrice,
    					
    					'fqgyQty' => $ranyouData['noqigang'],
    					'fqgyPrice'=>$QiGangPrice,
    					'fqgyAmount'=>$ranyouData['noqigang']*$QiGangPrice ,
    					
    					'ftrqQty'  => $ranyouData['noranqi'],
    					'ftrqPrice'=> $RanqiPrice,
    					'ftrqAmount'=> $ranyouData['noranqi'] * $RanqiPrice,
    					'fAllAmount'=> round($ranyouData['noranyou']*$ChaiYouPrice + $ranyouData['noqigang']*$QiGangPrice+ $ranyouData['noranqi'] * $RanqiPrice,2),
    					
    					
    			];
    			//     		 dd($SCR27013 , $SCR27014 );
    			//油耗 （SCR或者EGR油气首台!K2*1+(生效台数-1）*SCR或者EGR油气非首台!K2）/生效台数
    			$youhaoRY =round( ($SCR27002['fAllAmount'] + ($result['fqty']-1)* $SCR27003['fAllAmount']  )/$result['fqty'],2) ;
    			//证书费取船级社的NOX排放费的金额
    			$zhengshuf = 0 ;
    			$SCR27015 =[
    					'fbillno'=>$fbillno,
    					'fmodel'=> $fmodel,
    					'fyouhao'=> $youhaoRY ,
    					'fnaohqty'=>$ranyouData['naoh'],
    					'fnaohPrice'=>$fnaohPrice,
    					'fnaohAmount'=>$ranyouData['naoh']* $fnaohPrice,
    					
    					'fftqty'=>$result['fqty'],
    					'fgongshi'=> $result['ficergs'],
    					'frengongcb'=> $result['ficergs']*$fsetZhuangShiRGprice ,
    					
    					'fdftfeiyong1'=> $result['ficertjgzfy']+$result['fgdzjqq'], //ICER台架改造费
    					'ffttaishu1'=> $sxfqtyTmp ,
    					'fftfeiyong1'=>round(($result['ficertjgzfy']+$result['fgdzjqq'])/$sxfqtyTmp ,2),
    					
    					'fdftfeiyong2'=> 0 ,   // ==暂时
    					'ffttaishu2'=> $sxfqtyTmp,
    					'fftfeiyong2'=> 0/$sxfqtyTmp,
    					
    					'ffttaishu'=> $result['fqty'],
    					'fpfcsfei'=> $FpfcsfICER,
    					'fzsfei'=>$result['fchaunjishefy'] ,
    					'fdfttaishu'=>  $result['fqty'],
    					
    					'fftpfcsfei'=>round(($FpfcsfICER+$result['fchaunjishefy'])/$result['fqty'] ,2), //fpfcsfei+ fzsfei   ==暂时
    					'fdianfei'=>$fdianfei,
 
    			];
//     			dd($SCR27015,$request['fchaunjishefy']);
    			//fyouhao +fnaohAmount+ frengongcb+ fftfeiyongegr + fftpfcsfei+ fdianfei
    			$SCR27015['fAllAmount']= $SCR27015['fyouhao']+ $SCR27015['fnaohAmount']+ $SCR27015['frengongcb']
    			+$SCR27015['fftfeiyong1']+$SCR27015['fftfeiyong2'] +  $SCR27015['fftpfcsfei']+ $SCR27015['fdianfei'];
    			//     			  dd($SCR27012, $SCR27013,$SCR27014);
    			SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzICERRanYouHZ' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    			SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzICERrybyouqist' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    			SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzICERrybyouqiNOst' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    			
    			SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzICERRanYouHZ' , $SCR27015);
    			SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzICERrybyouqist' , $SCR27002);
    			SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzICERrybyouqiNOst' , $SCR27003);
    			
    			
    		}
//     		dd(222);
    		/* ICER燃气版油气     27005
    		gz_YjzICERrqbyouqist
    		ICER燃气版油气非首台  27004
    		gz_YjzICERrqbyouqiNOst
    		ICER其他成本     27006
    		gz_YjzICERQT */
    		//燃气版
    		if($result['fisryrq'] == '燃气'){
    			$SCR27005 =[
    					'fbillno'=>$fbillno,
    					'fmodel'=>$fmodel,
    					'fcyQty'  => $ranyouData['ranyou'],
    					'fcyPrice'=> $ChaiYouPrice,
    					'fcyAmount'=>$ranyouData['ranyou']*$ChaiYouPrice,
    					
    					'fqgyQty' => $ranyouData['qigang'],
    					'fqgyPrice'=>$QiGangPrice,
    					'fqgyAmount'=>$ranyouData['qigang']*$QiGangPrice ,
    					
    					'ftrqQty'  => $ranyouData['ranqi'],
    					'ftrqPrice'=> $RanqiPrice,
    					'ftrqAmount'=> $ranyouData['ranqi'] * $RanqiPrice,
    					'fAllAmount'=> round($ranyouData['ranyou']*$ChaiYouPrice + $ranyouData['qigang']*$QiGangPrice+ $ranyouData['ranqi'] * $RanqiPrice,2),
    					
    			];
    			//     		dd($SCR27009 , $result['fisryrq'] , $result['fisscr'] ,$result['fisegr'] );
    			// 燃气版非首台
    			$SCR27004 =[
    					'fbillno'=>$fbillno,
    					'fmodel'=>$fmodel,
    					'fcyQty'  => $ranyouData['noranyou'],
    					'fcyPrice'=> $ChaiYouPrice,
    					'fcyAmount'=>$ranyouData['noranyou']*$ChaiYouPrice,
    					
    					'fqgyQty' => $ranyouData['noqigang'],
    					'fqgyPrice'=>$QiGangPrice,
    					'fqgyAmount'=>$ranyouData['noqigang']*$QiGangPrice ,
    					
    					'ftrqQty'  => $ranyouData['noranqi'],
    					'ftrqPrice'=> $RanqiPrice,
    					'ftrqAmount'=> $ranyouData['noranqi'] * $RanqiPrice,
    					'fAllAmount'=> round($ranyouData['noranyou']*$ChaiYouPrice + $ranyouData['noqigang']*$QiGangPrice+ $ranyouData['noranqi'] * $RanqiPrice,2),
    					
    					
    			];
    			//     		 dd($SCR27013 , $SCR27014 );
    			//油耗 （SCR或者EGR油气首台!K2*1+(生效台数-1）*SCR或者EGR油气非首台!K2）/生效台数
    			$youhaoRanqi =round( ($SCR27005['fAllAmount'] + ($result['fqty']-1)* $SCR27004['fAllAmount']  )/$result['fqty'],2) ;
    			//证书费取船级社的NOX排放费的金额
    			$zhengshuf = 0 ;
    			$SCR27006 =[
    					'fbillno'=>$fbillno,
    					'fmodel'=> $fmodel,
    					'fyouhao'=> $youhaoRanqi ,
    					'fnaohqty'=>$ranyouData['naoh'],
    					'fnaohPrice'=>$fnaohPrice,
    					'fnaohAmount'=>$ranyouData['naoh']* $fnaohPrice,
    					
    					'fftqty'=>$result['fqty'],
    					'fgongshi'=> $result['ficergs'],
    					'frengongcb'=> $result['ficergs']*$fsetZhuangShiRGprice ,
    					
    					'fdftfeiyong1'=> $result['ficertjgzfy']+$result['fgdzjqq'], //ICER台架改造费
    					'ffttaishu1'=> $sxfqtyTmp ,
    					'fftfeiyong1'=>round(($result['ficertjgzfy']+$result['fgdzjqq'])/$sxfqtyTmp ,2),
    					
    					'fdftfeiyong2'=> 0 ,   // ==暂时
    					'ffttaishu2'=> $sxfqtyTmp,
    					'fftfeiyong2'=> 0/$sxfqtyTmp,
    					 
    					'ffttaishu'=> $result['fqty'],
    					'fpfcsfei'=> $FpfcsfICER,
    					'fzsfei'=> $result['fchaunjishefy'],
    					'fdfttaishu'=>  $result['fqty'],
    					
    					'fftpfcsfei'=>round(($FpfcsfICER+$result['fchaunjishefy'])/$result['fqty'] ,2), //fpfcsfei+ fzsfei   ==暂时
    					'fdianfei'=>$fdianfei,
    			];
    			//fyouhao +fnaohAmount+ frengongcb+ fftfeiyongegr + fftpfcsfei+ fdianfei
    			$SCR27006['fAllAmount']= $SCR27006['fyouhao']+ $SCR27006['fnaohAmount']+ $SCR27006['frengongcb']
    			+$SCR27006['fftfeiyong1']+$SCR27006['fftfeiyong2'] +  $SCR27006['fftpfcsfei']+ $SCR27006['fdianfei'];
    			
    			//     			  dd($SCR27012, $SCR27013,$SCR27014);
    			SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzICERQT' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    			SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzICERrqbyouqist' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    			SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzICERrqbyouqiNOst' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    			
    			SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzICERQT' , $SCR27006);
    			SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzICERrqbyouqist' , $SCR27005);
    			SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzICERrqbyouqiNOst' , $SCR27004);
    			
    			
    		}
    		
    		//汇总数据 
    		$ranyouHZTemp =[
    				'fbillno'=>$fbillno,
    				'fmodel'=> $fmodel,
    				'fSCRAmount' =>$SCR27001['fAllAmountSCR'],
    				'fEGRAmount' =>$SCR27001['fAllAmountEGR'],
    				'fEcoEGRAmount'  => $SCR27012['fAllAmount'],
    				'fICERRYAmount'  => $SCR27015['fAllAmount'],
    				'fICERRQAmount'  => $SCR27006['fAllAmount'],
    		];
    		SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzSCREGRHZ' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
    		SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzSCREGRHZ' , $ranyouHZTemp);
    		 
    		
	    		
    		////////////////////////
    		//判断是否是甲醇机
    		if( stripos( strtoupper($fmodel) , 'LGIM' ) !== false){
    			$result = [];
	    		//1-- 甲醇机
	    		$sql="  select v1.fbillno,v1.fmodel,v1.fsxtaishu,v1.fqty,v1.fsjpower,v1.fhtpower,v1.fchuanjishe,v1.fzhujiranliaos,
	    		v1.fisryrq,v1.fisscr,v1.fscrlowup,v1.fisegr,v1.fegrlowup,v1.fmpctriton,
	    		u1.fbrand,u1.fgas as franqi,u1.fdieseloil as franyou,u1.fcylinderoil as fqigangyou,u1.fhanjiegs,u1.fjijiags,
	    		u1.fzhuangshigs,u1.fegrgs,u1.fscrgs,u1.fftfscr,u1.fftfegr,u1.fftfiscr,u1.ffuliao,u1.fpackage,u1.ffuliaopakeage,
				 {$zdsql}  
	    		,jchz.fbillno as fbillnojchz, jchz.fstranyouyjcjegr,jchz.fstranyouegr,
	    		jchz.fstjiachunjijcjegr,jchz.fstjiachunjiegr,
	    		jchz.fstqigangjcjegr,jchz.fstqigangegr,
	    		jchz.fstnaohjcjegr,
	    		jchz.fnostranyouyjcjegr,jchz.fnostranyouegr,
	    		jchz.fnostjiachunjijcjegr,jchz.fnostjiachunjiegr,
	    		jchz.fnostqigangjcjegr,jchz.fnostqigangegr,
	    		jchz.fnostnaohjcjegr
	    		from gz_YBaseSC  v1  inner join gz_YBaseJS u1 on v1.fmodel = u1.fmodel
	    		left join gz_YjiachunHZ jchz on  jchz.fbillno = v1.fbillno and v1.fmodel = jchz.fmodel
				 $chuanjianfSql  
	    		where 1=1 and v1.fmodel ='{$fmodel}' and v1.fbillno ='{$fbillno}' " ;
	    		
	    		$result = SqlServerUtil::execTosqlOK($this->SqlServer,  $sql,2);
	    		if(empty($result)){
	    			showMsg(400,'甲醇机数据有误！');
	    		}
	    		//判断这里如果生效台数大于4的，分摊的台数是4；小于等于4，按照实际生效台数
	    		$sxfqtyTmp = $result['fqty'] ; 
	    		if($result['fqty']>4){
	    			$sxfqtyTmp = 4 ;
	    		}
	    		
	    		//判断 甲醇汇总 是否 已生成
	    		if(empty($result['fbillnojchz'])){
	    			showMsg(400,'流水号：'.$result['fbillnojchz'].'，还没有生成甲醇汇总，请联系技术');
	    		}
	    		//参数
	    		$CsData = $this->getSystemProfile(['FSetJiaChunZSF','FSetJiaChunDianFei','FSetJiaChunPFCSF',
	    				'FSetRGzhuangshiCJPrice','FSetChaiYouPrice','FSetQiGangPrice','FSetJIaChunPrice','fSetNaOHPrice',
	    				'FSetJIaChunDFTF',
	    		]);
	    		//油耗 （甲醇机首台!K2*1+(生效台数-1）*甲醇机油气非首台!K2）/生效台数
	    		//甲醇机首台
	    		$jcst = $result['fstranyouyjcjegr']*$CsData['fsetchaiyouprice']+ $result['fstjiachunjijcjegr']* $CsData['fsetjiachunprice']+ $result['fstqigangjcjegr']*$CsData['fsetqigangprice'] ;
	    		//甲醇机非首台
	    		$jcnost = $result['fnostranyouyjcjegr']*$CsData['fsetchaiyouprice']+ $result['fnostjiachunjijcjegr']* $CsData['fsetjiachunprice']+ $result['fnostqigangjcjegr']*$CsData['fsetqigangprice']  ;
	    		
	    		$youhaoJC = round( ($jcst + ($result['fqty']-1)* $jcnost  )/$result['fqty'],2) ;
	    		
	    		//甲醇机 不含EGR 数量
	    		$jcst1 =  $result['fstjiachunjiegr']  ;
	    		$jcnost1 =   $result['fnostjiachunjiegr']   ;
	    		$youhaoJC1 = round( ($jcst1 + ($result['fqty']-1)* $jcnost1  )/$result['fqty'],2) ;
	    		
	    		$JC27011 =[
	    				'fbillno'=>$fbillno,
	    				'fmodel'=> $fmodel,
	    				'fyouhao'=> $youhaoJC ,
	    				
	    				'fjiachunqty'=> $youhaoJC1 ,
	    				'fjiachunprice'=> $CsData['fsetjiachunprice'],
	    				'fjiachunamount'=> $youhaoJC1 *$CsData['fsetjiachunprice'],
	    				'fnaohqty'=>$result['fstnaohjcjegr'],
	    				'fnaohprice'=>$CsData['fsetnaohprice'],
	    				'fnaohamount'=>$result['fstnaohjcjegr']* $CsData['fsetnaohprice'],
	    				
	    				'fgongshi'=> $result['fegrgs'],
	    				'frengongcb'=> $result['fegrgs']*$CsData['fsetrgzhuangshicjprice'] ,
	    				'fdftfeiyong1'=> $CsData['fsetjiachundftf'] ,
	    				'fdftfeiyong2'=> $result['fftfegr'] ,
	    				'ffttaishu2'=> $sxfqtyTmp,
	    				'fftfeiyong2'=> round($result['fftfegr']/$sxfqtyTmp ,2) ,
	    				
	    				'fpfcsfei'=> $CsData['fsetjiachunpfcsf'] ,
	    				'fzsfei'=>  $result['fchaunjishefy']  ,//甲醇机证书费    ， $CsData['fsetjiachunzsf']
	    				'fdfttaishu'=>  $result['fqty'],
	    				'fftpfcsfei'=>round(($CsData['fsetjiachunpfcsf']+ $result['fchaunjishefy'] )/$result['fqty'] ,2), //fpfcsfei+ fzsfei   ==暂时
	    				'fdianfei'=> $CsData['fsetjiachundianfei'] ,
	    				
	    		];
	    		$JC27011['fallamount']= $JC27011['fyouhao'] +$JC27011['fnaohamount']+$JC27011['frengongcb']+ $JC27011['fdftfeiyong1']
	    		+$JC27011['fftfeiyong2'] +$JC27011['fftpfcsfei']+ $JC27011['fdianfei'] ;
	    		
	    		$JC27011yh=[
	    			'fbillno'=>$fbillno,
	    			'fmodel'=> $fmodel,
	    			'fqty'=>  $result['fqty'],
	    			'fstranyouyjcjegr' => $result['fstranyouyjcjegr'] ,
	    			'fsetchaiyouprice' =>$CsData['fsetchaiyouprice'],
	    			'fstjiachunjijcjegr' => $result['fstjiachunjijcjegr'] ,
	    			'fsetjiachunprice' =>$CsData['fsetjiachunprice'],
	    			'fstqigangjcjegr'=> $result['fstqigangjcjegr'] ,
	    			'fsetqigangprice' =>$CsData['fsetqigangprice'],
	    			'fstamount' => $jcst,
	    				
	    			'fnostranyouyjcjegr' => $result['fnostranyouyjcjegr'] ,
	    			'fnostjiachunjijcjegr'=> $result['fnostjiachunjijcjegr'] ,
	    			'fnostqigangjcjegr'	=> $result['fnostqigangjcjegr'] ,
	    			'fnostamount' => $jcnost,
	    				
	    		];
	    		//$JC27011yh['fAllAmount'] = $jcst + $jcnost ;
	    		
	    		SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzJiaChunQT' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
	    		$jsins = SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzJiaChunQT' , $JC27011);
	    		
	    		SqlServerUtil::sqlsrvDeleteBack($this->SqlServer,'gz_YjzJiaChunQTYouHao' , ['fbillno'=> "'".$result['fbillno']."'" ],true);
	    		$jsins = SqlServerUtil::sqlsrvCreate($this->SqlServer ,'gz_YjzJiaChunQTYouHao' , $JC27011yh);
	    		
	    	}
	    	showMsg(0);
	       
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
      
    
    
	

}
